package com.andy.blog.dal.service.impl;

import com.andy.blog.common.enums.DeleteTypeEnum;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.ArticleDO;
import com.andy.blog.dal.domain.ArticleTagInfoDO;
import com.andy.blog.dal.mapper.TArticleMapper;
import com.andy.blog.dal.service.ArticleDalService;
import com.andy.blog.dal.vo.ArticleVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2023/6/22 18:40
 */
@Service
public class ArticleDalServiceImpl implements ArticleDalService {

    @Resource
    private TArticleMapper tArticleMapper;


    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<ArticleDO> buildQueryWrapper(ArticleVO articleVO) {
        return new LambdaQueryWrapper<ArticleDO>()
                .like(StringUtils.isNotBlank(articleVO.getArticleTitle()), ArticleDO::getArticleTitle, articleVO.getArticleTitle())
                .eq(StringUtils.isNotBlank(articleVO.getStatus()), ArticleDO::getStatus, articleVO.getStatus())
                .eq(StringUtils.isNotBlank(articleVO.getIsRecommend()), ArticleDO::getIsRecommend, articleVO.getIsRecommend())
                .eq(StringUtils.isNotBlank(articleVO.getIsDeleted()), ArticleDO::getIsDeleted, articleVO.getIsDeleted())
                .orderByDesc(ArticleDO::getId);
    }

    @Override
    public IPage<ArticleDO> selectListByPage(ArticleVO articleVO) {
        LambdaQueryWrapper<ArticleDO> lambdaQueryWrapper = buildQueryWrapper(articleVO);
        IPage<ArticleDO> page = new Page<>(articleVO.getPageNum(), articleVO.getPageSize());
        return tArticleMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public IPage<ArticleDO> listByPage(ArticleVO articleVO) {
        IPage<ArticleDO> page = new Page<>(articleVO.getPageNum(), articleVO.getPageSize());
        LambdaQueryWrapper<ArticleDO> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper
                .like(StringUtils.isNotBlank(articleVO.getArticleTitle()), ArticleDO::getArticleTitle, articleVO.getArticleTitle())
                .like(StringUtils.isNotBlank(articleVO.getArticleType()), ArticleDO::getArticleType, articleVO.getArticleType())
                .eq(ArticleDO::getIsDeleted, DeleteTypeEnum.DELETE_N.getCode())
                .orderByDesc(ArticleDO::getIsTop)
                .orderByDesc(ArticleDO::getId);

        return tArticleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<ArticleDO> selectList(ArticleVO articleVO) {
        LambdaQueryWrapper<ArticleDO> lambdaQueryWrapper = buildQueryWrapper(articleVO);
        return tArticleMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public Long addArticle(ArticleVO articleVO) {
        ArticleDO articleDO = TransformUtils.transform(articleVO, ArticleDO.class);
        if (null == articleDO) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }
        articleDO.setCreateBy(SecurityUtils.getUsername());
        articleDO.setIsDeleted(DeleteTypeEnum.DELETE_N.getCode());

        try {
            tArticleMapper.insert(articleDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        return articleDO.getId();
    }

    @Override
    public ArticleDO selectById(Long id) {
        return tArticleMapper.selectById(id);
    }

    @Override
    public void updateById(ArticleDO articleDO) {
        if (null == articleDO) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }


        int row;
        try {
            row = tArticleMapper.updateById(articleDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }
        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101);
        }
    }

    @Override
    public ArticleTagInfoDO selectArticleHomeById(Long articleId) {
        return tArticleMapper.selectArticleHomeById(articleId);
    }

    @Override
    public Long selectArticleCount(ArticleVO articleVO) {
        LambdaQueryWrapper<ArticleDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return tArticleMapper.selectCount(lambdaQueryWrapper);
    }

    @Override
    public boolean checkArticleExist(Long articleId) {
        LambdaQueryWrapper<ArticleDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ArticleDO::getId, articleId)
                .eq(ArticleDO::getIsDeleted, DeleteTypeEnum.DELETE_N.getCode());

        ArticleDO articleDO = tArticleMapper.selectOne(lambdaQueryWrapper);
        return Objects.nonNull(articleDO);
    }
}
